package com.yiqi.liebang.feature.common.view;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.RxHelper;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.SealAppContext;
import com.yiqi.liebang.entity.bo.HomeBo;
import com.yiqi.liebang.feature.home.view.BannerWebActivity;
import com.yiqi.liebang.framework.api.HomeApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;


public class SplashActivity extends BaseActivity {

    RxPermissions rxPermissions;
    @BindView(R.id.iv_splash)
    ImageView mIvSplash;

    private static final int LOADING = R.drawable.ic_splash;
    private static final int ERROR = R.drawable.ic_splash;
    @BindView(R.id.tag_skip)
    TextView mTagSkip;
    private boolean mIsSkip = false;
    HomeBo.IndexBannerBean indexBannerBean;

    //默认配置
    private RequestOptions options = new RequestOptions()
            .placeholder(LOADING)
            .error(ERROR)
            .diskCacheStrategy(DiskCacheStrategy.ALL);

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initInjector() {



    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initData() {
        rxPermissions = new RxPermissions(this);
//        onCodeClick();
        getSplashData();

    }

    @OnClick(R.id.tag_skip)
    public void onClick() {
        _doSkip();
    }


    public void onCodeClick() {
        new Thread(new Runnable() {
            @Override
            public void run() {


            }
        }).start();
    }

    @Override
    protected void initView() {
        RxHelper.countdown(4)
                .compose(this.<Integer>bindToLife()).subscribe(observer);
    }

    private void getSplashData() {
        AM.api().createApiService(HomeApi.class)
                .getAdvertisement()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<HomeBo.IndexBannerBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HomeBo.IndexBannerBean s) {
                        indexBannerBean = s;

                        if (indexBannerBean != null) {
                            Glide.with(SplashActivity.this)
                                    .load(s.getBannerUrl())
                                    .apply(options)
                                    .into(mIvSplash);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    @OnClick(R.id.iv_splash)
    public void onViewClicked() {
        _doSkip();
        Intent intent = new Intent(this, BannerWebActivity.class);
        intent.putExtra("banner", indexBannerBean);
        startActivity(intent);
    }


    //创建一个下游 Observer
    Observer<Integer> observer = new Observer<Integer>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Integer value) {
//            _doSkip()

            mTagSkip.setText("跳过 " + (value + 1) + "s");
        }

        @Override
        public void onError(Throwable e) {
            _doSkip();
        }

        @Override
        public void onComplete() {
            _doSkip();
        }
    };

    private void _doSkip() {
        if (!mIsSkip) {
            mIsSkip = true;
            rxPermissions
                    .requestEach(Manifest.permission.CAMERA,
                            Manifest.permission.CALL_PHONE,
                            Manifest.permission.READ_PHONE_STATE)
                    .subscribe(permission -> { // will emit 2 Permission objects
                        if (permission.granted) {

                        } else if (permission.shouldShowRequestPermissionRationale) {

                        } else {

                        }
                        if (AM.user().isLogin()) {
                            RongIM.connect(AM.user().getLoginResultCache().getRongCloudToken(), SealAppContext.getInstance().getConnectCallback());
                        }

                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    });
        }
    }

}
